package using.student;//框体

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.SQLException;

class student_manage extends JFrame
{
    public int i=0;
    public student_manage()
    {
        setTitle("Student information query interface");
        Container container=getContentPane();
        container.setLayout(new GridLayout(4,1));
        setBounds(20,20,1000,1000);

        JLabel jl1=new JLabel("Student number");
        JTextField jt1=new JTextField();

        JButton jb1=new JButton("update");
        JButton jb2=new JButton("new");

        JPanel panel1=new JPanel();
        panel1.setBorder(BorderFactory.createTitledBorder("Management interface"));
        panel1.setLayout(null);

        JLabel jl2=new JLabel(" No.:");
        JLabel jl3=new JLabel(" Name:");
        JLabel jl4 = new JLabel("Class:");
        JLabel jl5 = new JLabel("Sex:" );
        JLabel jl6 = new JLabel("Faculty:");
        JLabel jl7=new JLabel("Major:");
        JLabel jl8=new JLabel("Association:");

        JTextField jt2 = new JTextField();
        JTextField jt3 = new JTextField();
        JTextField jt4 = new JTextField();
        JTextField jt5 = new JTextField();
        JTextField jt6 = new JTextField();
        JTextField jt7 = new JTextField();
        JTextField jt8 = new JTextField();

        jl1.setBounds(50, 100, 100, 30);
        jt1.setBounds(160, 100, 70, 30);

        jb1.setBounds(240, 100, 70, 30);
        jb2.setBounds(900, 40, 80, 30);

        jl2.setBounds(400, 30, 70, 28);
        jt2.setBounds(460, 30, 70, 28);

        jl3.setBounds(400, 80, 70, 28);
        jt3.setBounds(460, 80, 70, 28);

        jl4.setBounds(400, 130, 70, 28);
        jt4.setBounds(460, 130, 70, 28);

        jl5.setBounds(400, 180, 70, 28);
        jt5.setBounds(460, 180, 70, 28);

        jl6.setBounds(600, 30, 100, 28);
        jt6.setBounds(700, 30, 100, 28);

        jl7.setBounds(600, 80, 100, 28);
        jt7.setBounds(700, 80, 100, 28);

        jl8.setBounds(600, 130, 100, 28);
        jt8.setBounds(700, 130, 100, 28);

        panel1.add(jl1);
        panel1.add(jt1);
        panel1.add(jb1);
        panel1.add(jb2);
        panel1.add(jl2);
        panel1.add(jl3);
        panel1.add(jl4);
        panel1.add(jl5);
        panel1.add(jl6);
        panel1.add(jl7);
        panel1.add(jl8);
        panel1.add(jt2);
        panel1.add(jt3);
        panel1.add(jt4);
        panel1.add(jt5);
        panel1.add(jt6);
        panel1.add(jt7);
        panel1.add(jt8);

        jb1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                String str1,str2,str3,str4,str5,str6,str7;
                str1 = jt2.getText();
                str2 = jt3.getText();
                str3 = jt4.getText();
                str4 = jt5.getText();
                str5 = jt6.getText();
                str6 = jt7.getText();
                str7 = jt8.getText();
                String str8 = "update member set name=?,clname=?,sex=?,department=?,sdept=?,community=? where sno='"
                        + jt1.getText() + "'";
                try
                {
                    SQL_sever.con= SQL_sever.connection();
                    PreparedStatement sql= SQL_sever.con.prepareStatement(str8);
                    sql.setString(1, str2);
                    sql.setString(2, str3);
                    sql.setString(3, str4);
                    sql.setString(4, str5);
                    sql.setString(5, str6);
                    sql.setString(6, str7);
                    int result = sql.executeUpdate();
                    validate();
                }
                catch (SQLException e1)
                {
                    e1.printStackTrace();
                }
                String[]column={"No","Name","Class","Sex","Faculty","Major","Association"};
                String[][] tables={{str1,str2,str3,str4,str5,str6,str7}};

                JTable table=new JTable(tables,column);
                JScrollPane scrollPane2=new JScrollPane(table);
                scrollPane2.setBorder(BorderFactory.createTitledBorder("new message"));
                container.add(scrollPane2,BorderLayout.CENTER);
                JOptionPane.showMessageDialog(null,"Update succeeded");
                validate();
            }
        });

        jb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String str3, str4, str5, str6, str7, str9, str10 = null;
                boolean exist = true;
                String str1 = jt2.getText();
                String str2 = jt3.getText();
                str3 = jt4.getText();
                str4 = jt5.getText();
                str5 = jt6.getText();
                str6 = jt7.getText();
                str7 = jt8.getText();
                String str11 = "select cname from [community] where cname='" + str7 + "'";

                try {
                    SQL_sever.sql = SQL_sever.con.createStatement();
                    SQL_sever.res =SQL_sever.sql.executeQuery(str11);
                    while (SQL_sever.res.next())
                    {
                        str10 = SQL_sever.res.getString("cname");
                    }
                }
                catch (SQLException e1)
                {
                    e1.printStackTrace();
                }

                if (str1.equals("") || str2.equals("") || str7.equals(""))
                {
                    JOptionPane.showMessageDialog(null, "Student number, name and association cannot be blank");
                }
                else
                {
                    str9 = "insert into member values(?,?,?,?,?,?,?)";
                    if (str10 != null) {
                        try {
                            SQL_sever.con = SQL_sever.connection();
                            PreparedStatement sql = SQL_sever.con.prepareStatement(str9);
                            sql.setString(1, str1);
                            sql.setString(2, str2);
                            sql.setString(3, str3);
                            sql.setString(4, str4);
                            sql.setString(5, str5);
                            sql.setString(6, str6);
                            sql.setString(7, str7);
                            int result = sql.executeUpdate();
                        }
                        catch (SQLException e1)
                        {
                            e1.printStackTrace();
                        }
                        JOptionPane.showMessageDialog(null, "Insert successful");
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(null, "The association does not exist");
                    }
                }
            }
        });

        container.add(panel1);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setVisible(true);
    }
    public static void tan()
    {
        student_manage menbers_student_manageS =new student_manage();
        menbers_student_manageS.setVisible(true);
    }
}
